﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class SettingController : Controller
    {
        public ISettingService SettingService { get; set; }

        [CheckPermission("Setting.List")]
        // GET: Setting
        public ActionResult List()
        {
            var settings = SettingService.GetAll();
            return View(settings);
        }

        [CheckPermission("Setting.Add")]
        public ActionResult AddNew()
        {
            return View();
        }

        [CheckPermission("Setting.Add")]
        [HttpPost]
        public ActionResult AddNew(string key, string value, string describle)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            var Setting = SettingService.GetSettingByKey(key);

            if (Setting != null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = key + "已存在" });
            }

            long settingId = SettingService.AddNew(key, value, describle);

            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("Setting.Edit")]
        [HttpGet]
        public ActionResult Edit(long id)
        {
            var Setting = SettingService.GetSettingById(id);

            if (Setting == null)
            {
                return Content("未找到ID为" + id + "的配置");
            }

            return View(Setting);
        }

        [CheckPermission("Setting.Edit")]
        [HttpPost]
        public ActionResult Edit(long id, string key, string value, string describle)
        {
            //验证数据
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            //获取数据判断配置是否存在
            var Setting = SettingService.GetSettingById(id);

            if (Setting == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未找到ID为" + id + "的配置,请检查是否已被删除！" });
            }

            //获取名称判断修改的配置是否已存在并且不为自身原有名称
            var dataSetting = SettingService.GetSettingByKey(key);

            if (dataSetting != null && Setting.Id != dataSetting.Id)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = key + "配置已存在" });
            }

            SettingService.Update(id, key, value, describle);

            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("Setting.Delete")]
        [HttpPost]
        public ActionResult Delete(long settingId)
        {
            SettingService.MarkDeleted(settingId);

            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("Setting.Delete")]
        [HttpPost]
        public ActionResult BatchDel(long[] settingIds)
        {
            foreach (var settingId in settingIds)
            {
                SettingService.MarkDeleted(settingId);
            }

            return Json(new AjaxResult { Status = "ok" });
        }
    }
}